/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { QuestionVo } from "~/question/api/question.ts";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";
import { useMessage } from "@/hooks/useMessage.ts";
import { deleteByIds, levels, question_types } from "~/question/api/question";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { ElTable, ElTableColumn, ElTag } from "element-plus";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const dictStore = useDictStore();
  const msg = useMessage();

  const showBtn = (auth: string | string[], row: QuestionVo) => {
    return hasAuth(auth);
  };

  return [
    // 多选列
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
      selectable: (row: QuestionVo) => ![1].includes(row.id as number),
    },
    // 索引序号列
    { type: "index" },
    {
      label: () => t("question-manage.group"),
      prop: "group.name",
      cellRender({ row }) {
        return <ElTag>{row.group.name}</ElTag>;
      },
    },
    {
      label: () => t("question-manage.level"),
      prop: "level",
      minWidth: 120,
      cellRender({ row }) {
        const lv = levels.find((lv) => lv.level == row.level);
        return <ElTag>{lv?.name || row.level}</ElTag>;
      },
    },
    {
      label: () => t("question-manage.type"),
      prop: "type",
      minWidth: 120,
      cellRender({ row }) {
        const type =
          question_types.find((type) => type.code == row.type)?.name ||
          row.type;
        return <ElTag>{type}</ElTag>;
      },
    },
    {
      label: () => t("question-manage.question"),
      prop: "title",
      minWidth: 120,
      cellRender({ row }) {
        const parse = new DOMParser();
        const title = parse.parseFromString(row.title, "text/html").body
          .textContent;
        return <div>{title}</div>;
      },
    },

    // {
    //   label: () => t("question-manage.analysis"),
    //   prop: "analysis",
    //   minWidth: 120,
    // },
    { label: () => t("common.created_at"), prop: "created_at" },
    { label: () => t("common.updated_at"), prop: "updated_at" },
    { label: () => t("common.creator"), prop: "creator.nickname" },
    {
      type: "operation",
      fixed: "right",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => showBtn("question:update", row),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            show: ({ row }) => showBtn("question:delete", row),
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
